package cn.ligui.exportsis.service;

import cn.ligui.exportsis.bean.AssetTagInfo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author: ligui
 * @CreateTime: 2023-08-07 10:40
 * @Description:
 */
@Slf4j
public class CommonService {

    public static List<AssetTagInfo> getTagList(){
        Set<AssetTagInfo> tagInfoSet = new HashSet<>();
        try (XSSFWorkbook workbook = new XSSFWorkbook(new File("Data_tag.xlsx"))){
            int numberOfSheets = workbook.getNumberOfSheets();
            XSSFCell cell;
            for (int i = 0; i < numberOfSheets; i++) {
                XSSFSheet sheet = workbook.getSheetAt(i);
                int lastRowNum = sheet.getLastRowNum();
                for (int j = 1; j <= lastRowNum; j++) {
                    XSSFRow row = sheet.getRow(j);
                    cell = row.getCell(3);
                    if (cell == null) {
                        continue;
                    }
                    String tag = cell.getStringCellValue();
                    cell = row.getCell(0);
                    if (cell == null) {
                        continue;
                    }
                    String name = cell.getStringCellValue();
                    if (StringUtils.isNotBlank(tag) && !"-".equals(tag)) {
                        AssetTagInfo assetTagInfo = new AssetTagInfo();
                        assetTagInfo.setSisTag(tag);
                        assetTagInfo.setName(name);
                        tagInfoSet.add(assetTagInfo);
                    }
                }
            }
        } catch (Exception e) {
            log.error("Failed to parse the xlsx file" + e);
        }
        return new ArrayList<>(tagInfoSet);
    }
}
